package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmy.cultivate.entity.Quarter;
import com.xmy.cultivate.service.IQuarterService;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.PageResult;
import com.xmy.cultivate.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 每年季度 前端控制器
 * </p>
 *
 * @author min
 * @since 2022-08-22
 */
@RestController
@RequestMapping("/api/quarter")
@Scope("prototype")
public class QuarterController extends BaseController{

    @Autowired
    IQuarterService iQuarterService;

    @GetMapping()
    public Result findAll(@RequestParam Map reMap){
        try{
            Integer pageNum = StringUtils.isEmpty((String)reMap.get("pageNum")) ? 1 : Integer.valueOf((String)reMap.get("pageNum"));
            Integer pageSize = StringUtils.isEmpty((String)reMap.get("pageSize")) ? 10: Integer.valueOf((String)reMap.get("pageSize"));

            IPage<Quarter> iPage = new Page<>(pageNum,pageSize);

            String yearPart = (String) reMap.get("yearPart");
            QueryWrapper<Quarter> queryWrapper =  new QueryWrapper<>();
            queryWrapper.eq(StringUtils.isNotBlank(yearPart),"year_part",yearPart);

            iQuarterService.findAll(iPage,queryWrapper);
            Quarter quarter = iQuarterService.getNowQuarter();
            for(Quarter quarterInfo : iPage.getRecords()){
                if (quarterInfo.getNum().equals(quarter.getId())){
                    quarterInfo.setIsCurrent(1);
                }else {
                    quarterInfo.setIsCurrent(0);
                }
            }


            return new Result(Code.OK,new PageResult(iPage.getRecords(),iPage.getTotal(),iPage.getPages()));
        }catch (Exception e){
            return new Result(Code.ERROR,e.getMessage());
        }
    }

    @GetMapping("yearPartList")
    public Result getYearPartList(){
        try{
            List<Map<String,Object>> mapList = iQuarterService.getYearPartList();
            return new Result(Code.OK,new PageResult(mapList,new Long(mapList.size()),1L));
        }catch (Exception e){
            return new Result(Code.ERROR,e.getMessage());
        }
    }

    public Result save(@RequestBody Quarter quarter){
        try{
            boolean rs = iQuarterService.saveTo(quarter);
            return new Result(rs?Code.OK:Code.ERROR);
        }catch (Exception e){
            return new Result(Code.ERROR,e.getMessage());
        }
    }

    /**
     *获取当前季度
     * @return
     */
    @GetMapping("/getNowQuarter")
    public Result getNowQuarter(){
        Quarter quarter = iQuarterService.getNowQuarter();
        return new Result(Code.OK,quarter);
    }

    /**
     * 通过年份获取季度
     * @return
     */
    @GetMapping("/getQuarterForYearPart")
    public Result getQuarterForYearPart(@RequestParam(value = "yearPart",defaultValue = "") String yearPart){
        if (StringUtils.isBlank(yearPart)){
            return new Result(Code.ERROR,"","年份不能为空");
        }
        QueryWrapper<Quarter> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("year_part",yearPart);
        queryWrapper.orderByAsc("sort");
        List<Quarter> quarterList = iQuarterService.list(queryWrapper);
        return new Result(Code.OK,new PageResult(quarterList,(long)quarterList.size(),1L),"成功");
    }
}
